import { createRouter, createWebHistory } from "vue-router";
import store from "@/store";
// import { staticRouter } from '../utils/routeData.js'
import LoginPage from "@/views/login.vue";
import MainPage from "@/views/mainPage.vue";
import RecommendManage from "@/views/info/recommendManage.vue";
import GoodsManage from "@/views/goods/goodsManage.vue";


const routes = [
  {
    path: "/login",
    show: false,
    name: '登录',
    iconCls: 'el-icon-platform-eleme',
    component: LoginPage
  },
  {
    path: "/", // 设置根路由（登录后默认进入的路由）指向主页面组件
    show: false,
    iconCls: 'el-icon-platform-eleme',
    name: '主页',
    component: MainPage,
  },
  {
    path: "/recommend",
    show: true,
    iconCls: 'el-icon-platform-eleme',
    name: '推荐管理',
    component: RecommendManage
  },
  {
    path: "/goodsManage",
    show: true,
    iconCls: 'el-icon-platform-eleme',
    name: '商品管理',
    component: GoodsManage
  },
  // 可以继续添加更多的子路由配置，对应不同的管理系统功能页面
  // 以下示例添加一个带有子菜单的路由配置（比如假设商品管理有子菜单分类管理和库存管理等，这里只是示例，你可按需替换真实内容）
  // {
  //   path: '/goodsManage',
  //   name: 'GoodsManage',
  //   component: GoodsManage,
  //   children: [
  //     {
  //       path: 'categoryManage', // 子路由路径，相对父路由，访问路径为 /goodsManage/categoryManage
  //       name: 'CategoryManage',
  //       component: () => import('@/views/CategoryManage.vue') // 引入对应的子组件，这里使用懒加载方式，可按需调整
  //     },
  //     {
  //       path: 'stockManage',
  //       name: 'StockManage',
  //       component: () => import('@/views/StockManage.vue')
  //     }
  //   ]
  // }
];

// routes.push(...staticRouter)

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
});

router.beforeEach((to,from, next) => {
  const isLoggedIn = store.getters.isUserLoggedIn; // 获取登录状态
  // console.log(isLoggedIn)
  if (to.name ==='main' &&!isLoggedIn) {
    next({ name: 'loginPage' });
  } else if (to.name === 'loginPage' && isLoggedIn) {
    next({ name:'main' });
  } else {
    next();
  }
});

export default router;